<?php
/**
 *
 *
 * This file is part of Webappkit, a packages system for PHP web apps
 * @link http://webappkit.net
 * @package dj
 * @subpackage forms
 * @author J.Ducastel <nospam0@ducastel.name>
 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
 */

/**
 * @property bool $required
 * @property string $label
 * @property string $help_text
 * @property mixed $initial initial value
 * @property DjWidget $widget widget to render HTML
 * @property array $error_messages available error messages type => error message
 *
 * implementation-specific properties
 * @property string $name name within parent form and html name
 * @property mixed $value value to render with, if set
 * @property string $error error message when bound
 */
class DjField extends DataBean {

    /**
     * accepts an associative array to set attributes
     * required, label, help_text, initial, widget, error_messages
     * @param array $params
     * /
    public function __construct($params=array()) {
        parent::__construct($params);
    }/**/

    /**
     * return the cleaned value
     * if value does not validate, raise a DjValidationError
     * @param mixed $input
     * @return mixed
     */
    public function clean($input) {
        $input = $this->_normalize($input);
        $this->_cleanRequired($input);
        return $input;
    }

    /**
     * bounds to data = sets name and value
     * @param string $name
     * @param mixed $value
     * @return bool
     */
    public function bound($name, $value=null) {
        $this->name = $name;
        $this->value = $value;
        // validating
        try {
            $this->clean($value);
        } catch (DjValidationError $e) {
            $this->error = $e->getMessage();
        }
        return true;
    }

    /**
     * html rendering
     * @uses DjWidget
     * @return string
     */
    public function __toString() {
        if (property_exists($this, 'value'))
            return $this->widget->render($this->name, $this->value);
        else
            return $this->widget->render($this->name, $this->initial);
    }

    protected $required = true;
    protected function _required_check($x) {
        return is_bool($x);
    }

    public $label;

    public $help_text;

    public $error;

    protected $initial;

    protected $widget;
    protected function _widget_check($x) {
        return is_a($x, 'DjWidget');
    }
    protected function _widget_autoValue() {
        return new DjTextInput();
    }

    protected $error_messages = array(
        'required' => 'This field is required'
        , 'invalid' => 'This is not a valid value');

    /**
     * raise DjValidationError if required is true and input is empty
     * @param mixed $value
     * @return true
     */
    protected function _cleanRequired($value) {
        if ($this->required and
        (is_null($value) or $value===false or $value==''))
            throw new DjValidationError($this->error_messages['required']);
        return true;
    }

    /**
     * normalize input value
     * @param mixed $value
     * @return mixed
     */
    protected function _normalize($value) {
        return $value;
    }
}